home *** CD-ROM | disk | FTP | other *** search
/ Macwelt 1 / Macwelt DVD 1.toast / Web-Publishing / HTML-Editoren / Alpha ƒ / Tcl / Modes / HTML and CSS Modes / cssMode.tcl < prev    next >
Encoding:
Text File  |  2001-01-12  |  7.6 KB  |  236 lines

  1. ## -*-Tcl-*-
  2.  # ###################################################################
  3.  #  CSS mode - tools for editing CSS documents
  4.  # 
  5.  #  FILE: "cssMode.tcl"
  6.  #                                    created: 97-03-01 17.02.41 
  7.  #                                last update: 00-12-29 23.45.25 
  8.  #  Author: Johan Linde
  9.  #  E-mail: <alpha_www_tools@go.to>
  10.  #     www: <http://go.to/alpha_www_tools>
  11.  #  
  12.  # Version: 2.0
  13.  # 
  14.  # Copyright 1997-2001 by Johan Linde
  15.  #  
  16.  # This program is free software; you can redistribute it and/or modify
  17.  # it under the terms of the GNU General Public License as published by
  18.  # the Free Software Foundation; either version 2 of the License, or
  19.  # (at your option) any later version.
  20.  # 
  21.  # This program is distributed in the hope that it will be useful,
  22.  # but WITHOUT ANY WARRANTY; without even the implied warranty of
  23.  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  24.  # GNU General Public License for more details.
  25.  # 
  26.  # You should have received a copy of the GNU General Public License
  27.  # along with this program; if not, write to the Free Software
  28.  # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  29.  # 
  30.  # ###################################################################
  31.  ##
  32.  
  33. #===============================================================================
  34. # This is the main file for CSS mode
  35. #===============================================================================
  36.  
  37. alpha::mode CSS 2.0 cssMode {*.css *.CSS} {
  38.     cssMenu electricBraces electricSemicolon electricReturn electricTab
  39. } {
  40.     addMenu cssMenu •150
  41. } uninstall {
  42.     if {[askyesno "This will uninstall both HTML and CSS modes. Continue?"] == "no"} {return}
  43.     set folder [file dirname [procs::find htmlMenu]]
  44.     if {![file exists $folder]} {return}
  45.     catch {file delete [file join $folder *]}
  46.     catch {file delete $folder}
  47.     catch {file delete [file join $HOME Tcl Completions HTMLCompletions.tcl]}
  48.     catch {file delete [file join $HOME Tcl Completions CSSCompletions.tcl]}
  49.     catch {file delete [file join $HOME Help "HTML Help"]}
  50.     catch {file delete [file join $HOME Help "HTML Help.tcl"]}
  51. } maintainer {
  52.     "Johan Linde" <alpha_www_tools@go.to> <http://go.to/alpha_www_tools>
  53. } help {file "HTML Help"}
  54.  
  55. proc cssMode {} {}
  56.  
  57. namespace eval css {}
  58. namespace eval html {}
  59.  
  60. newPref f wordWrap 0 CSS
  61. newPref v wordBreak {[-@\w]+} CSS
  62. newPref v wordBreakPreface {[^-@\w]} CSS
  63. # Color of comments
  64. newPref v commentColor red CSS
  65. # Color of CSS keywords
  66. newPref v keywordColor blue CSS
  67. # Color of HTML elements
  68. newPref v htmlColor magenta CSS
  69.  
  70. # Coloring
  71. proc css::Coloring {{changing 0}} {
  72.     global CSSmodeVars css::Property css::Descriptor
  73.     if {!$changing} {
  74.         regModeKeywords -b {/*} {*/} -c $CSSmodeVars(commentColor) CSS {}
  75.     }
  76.     regModeKeywords -a -k $CSSmodeVars(htmlColor) CSS [css::GetHtmlWords]
  77.     regModeKeywords -a -k $CSSmodeVars(keywordColor) CSS \
  78.       [concat [array names css::Property] [array names css::Descriptor] \
  79.       {@font-face important active after before first first-child first-letter 
  80.     first-line focus hover lang left link right visited}]
  81. }
  82.  
  83. proc css::ChangeColoring {flag} {
  84.     global CSSmodeVars
  85.     switch $flag {
  86.         htmlColor -
  87.         keywordColor {css::Coloring 1}
  88.         commentColor {regModeKeywords -a -c $CSSmodeVars(commentColor) CSS}
  89.     }
  90.     refresh
  91. }
  92.  
  93. # Load other CSS mode files.
  94. foreach __tmp {cssProperties htmlcssInit htmlMenuDefinition html40} {
  95.     if {[catch {${__tmp}.tcl}]} {
  96.         beep
  97.         alertnote "Loading of ${__tmp}.tcl failed"
  98.         return
  99.     }
  100. }
  101.  
  102. foreach __vvv [array names CSSmodeVars] {
  103.     set CSSmodeVarsInvisible($__vvv) 1
  104. }
  105.  
  106. unset __vvv
  107.  
  108. proc css::DividingLine {} {
  109.     insertText "/*=============================================================================*/\r"
  110. }
  111. Bind 'l' <C> css::DividingLine CSS
  112.  
  113.  
  114. proc CSS::electricSemi {} {
  115.     insertText ";\r" [text::indentString [getPos]]
  116. }
  117.  
  118. proc CSS::parseFuncs {} {
  119.     set pos [minPos]
  120.     set funcExpr {^[ \t]*([^\r\n\{]+)\{}
  121.     while {[set res [search -s -f 1 -r 1 -i 0 -n $funcExpr $pos]] != ""} {
  122.         if {[regexp $funcExpr [eval getText $res] dummy word]} {
  123.             lappend m [list $word [lindex $res 0]]
  124.         }
  125.         set pos [lindex $res 1]
  126.     }
  127.     return [join [lsort -ignore $m]]
  128. }
  129.  
  130. proc CSS::DblClick {from to} {
  131.     global css::IsDescriptor
  132.     if {![catch {search -s -f 0 -r 1 -m 0 {[ \t\r\n]+[^ \t\r\n;:]+[ \t\r\n]*:} $from} res] && [pos::compare [lindex $res 1] >= $to]} {
  133.         set css::IsDescriptor [css::IsInAtRule font-face]
  134.         css::Dialog [string tolower [string trim [eval getText $res] " \t\r\n:"]]
  135.     } else {
  136.         HTML::DblClick $from $to
  137.     }
  138. }
  139.  
  140. #===============================================================================
  141. # Preferences
  142. #===============================================================================
  143.  
  144. proc css::modifyFlags {} {
  145.     global CSSmodeVars HTMLmodeVars alpha::colors
  146.     set box "-t {CSS mode Preferences} 100 10 450 30 \
  147.       -c {Create missing file without asking when cmd-double-clicking a link} $HTMLmodeVars(createWithoutAsking) 10 35 460 50 \
  148.       -c {Cmd-double-clicking on non-text file link opens file} $HTMLmodeVars(openNonTextFile) 10 55 450 70 \
  149.       -t {Color of keywords:} 10 75 150 90 \
  150.       -m [list [concat $CSSmodeVars(keywordColor) ${alpha::colors}]] 160 75 310 95 \
  151.       -t {Color of comments:} 10 100 150 115 \
  152.       -m [list [concat $CSSmodeVars(commentColor) ${alpha::colors}]] 160 100 310 120 \
  153.       -t {Color of HTML:} 10 125 150 140 \
  154.       -m [list [concat $CSSmodeVars(htmlColor) ${alpha::colors}]] 160 125 310 145 \
  155.       -t wordBreak: 10 150 150 165 \
  156.       -e [list $CSSmodeVars(wordBreak)] 155 150 450 165 \
  157.       -t wordBreakPreface: 10 175 150 190 \
  158.       -e [list $CSSmodeVars(wordBreakPreface)] 155 175 450 190"
  159.     set values [eval [concat dialog -w 465 -h 235 -b OK 20 205 85 225 -b Cancel 110 205 175 225 $box]]
  160.     if {[lindex $values 1]} {return}
  161.     for {set i 0} {$i < 7} {incr i} {
  162.         set flag [lindex {createWithoutAsking openNonTextFile keywordColor commentColor htmlColor wordBreak wordBreakPreface} $i]
  163.         global $flag
  164.         set m CSS
  165.         if {$i < 2} {set m HTML}
  166.         set val [lindex $values [expr {$i + 2}]]
  167.         if {[set ${m}modeVars($flag)] != $val} {
  168.             if {[string match "word*" $flag]} {
  169.                 set $flag $val
  170.             }
  171.             set ${m}modeVars($flag) $val
  172.             prefs::modifiedModeVar $flag $m
  173.             if {[string match "*Color" $flag]} {css::ChangeColoring $flag}
  174.         }
  175.     }
  176. }
  177.  
  178. #===============================================================================
  179. # ◊◊◊◊ Initialization ◊◊◊◊ #
  180. #===============================================================================
  181.  
  182. # Define key bindings from html menu.
  183. proc css::BindingsFromMenu {me tmplist} {
  184.     global htmlMenuKey html${me}Sub
  185.     upvar $tmplist tmp
  186.     foreach it [set html${me}Sub] {
  187.         if {[llength $it] > 2} {
  188.             set elem [lindex $it 2]            
  189.              if {[info exists htmlMenuKey(${me}/[lindex $it 1])]} {
  190.                 set key $htmlMenuKey(${me}/[lindex $it 1])
  191.             } else {
  192.                 set key [lindex $it 0]
  193.             }
  194.             css::BindOneKey $key $elem "" tmp
  195.         }
  196.     }
  197. }
  198.  
  199. # Define key bindings.
  200. if {[catch {html::ReadCache "CSS keybindings cache"}]} {
  201.     html::ReadMenuKeys
  202.     message "Defining key bindings…"
  203.     foreach __tmp [lrange $htmlSubMenus $htmlStartElements end] {
  204.         css::BindingsFromMenu [lindex $__tmp 0] tmplist
  205.     }
  206.     if {[html::NewElementsExists]} {
  207.         html::CreateCustomSub
  208.         css::BindingsFromMenu Custom tmplist
  209.     }
  210.     html::SaveCache "CSS keybindings cache" $tmplist
  211.     unset tmplist
  212.     catch {unset htmlMenuKey}
  213. } else {
  214.     message "Reading key bindings…"
  215. }
  216.  
  217. css::Coloring
  218. rename css::BindingsFromMenu ""
  219. catch {unset __tmp}
  220.  
  221. # Change mode hook
  222. proc css::ChangeMode {args} {
  223.     css::DisableEnable on
  224. }
  225.  
  226. hook::register changeMode css::ChangeMode CSS
  227.  
  228. set CSS::commentCharacters(General) "*"
  229. set CSS::commentCharacters(Paragraph) [list "/* " " */" " * "]
  230. set CSS::commentCharacters(Box) [list "/*" 2 "*/" 2 "*" 3]
  231.  
  232. set cssModeIsLoaded 1
  233.  
  234. message "CSS initialization complete."
  235.  
  236.